package com.choosefine.base.shixiaobao.common.utils;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

import org.json.JSONObject;
import org.springframework.web.util.DefaultUriTemplateHandler;

import com.google.gson.Gson;

/**
 * @author 潘钱钦 上午11:25:14
 * 
 * @author liaozhanggen 2017-02-22 16:10 update code
 * 
 */
public class URLHandlerUtils {

	/**
	 * 组装路径
	 *
	 * @Author：liaozhanggen
	 * @Create Date：2017年2月22日
	 * @param：	rootURL 协议路径  eg: http://localhost:8080/shixiaobao/
	 * 			pathURL 访问的映射路径 eg: xxx/xxx
	 * @return：StringBuffer
	 */
	public static StringBuffer getUrlBuffer(String rootURL, String pathURL) {
		StringBuffer sb=new StringBuffer();
		sb.append(rootURL).append(pathURL);
		return sb;
	}
	
	public static String DefaultUriTemplateHandler(String uri,
			Map<String, Object> params) {
		DefaultUriTemplateHandler uriTemplateHandler = new DefaultUriTemplateHandler();
		String url = uriTemplateHandler.expand(uri, params) + "";
		return url;
	}
	public static String DefaultUriTemplateHandler(String uri){
		Map<String, Object> params = new HashMap<>();
		DefaultUriTemplateHandler uriTemplateHandler=new DefaultUriTemplateHandler();
		String url=uriTemplateHandler.expand(uri, params)+"";
		return url;
	}
	public static String urlFormat(String url, Map<String, Object> map) {
		StringBuffer str = new StringBuffer(url);
		if (map != null) {
			Iterator<String> mapFor = map.keySet().iterator();
			while (mapFor.hasNext()) {
				String key = mapFor.next();
				str.append("/");
				str.append(map.get(key));
			}
		}
		return str.toString();
	}

	/**
	 * 链接与参数拼接
	 * 
	 * @param url
	 * @param map
	 */
	public static String urlFormat(StringBuffer url, Map<String, Object> map) {
		if (map != null) {
			Iterator<String> mapFor = map.keySet().iterator();
			int num = 1;
			while (mapFor.hasNext()) {
				String key = mapFor.next();
				Object mapCont = map.get(key) == null ? "" : map.get(key);
				if (mapCont != null && mapCont != "") {
					if (num == 1) {
						url.append("?" + key + "=" + mapCont.toString());
					} else {
						url.append("&" + key + "=" + mapCont.toString());
					}
					num++;
				}
			}
		}
		return url.toString();
	}

	/**
	 * 2016.1212 dzq 链接与参数拼接
	 * 
	 * @param url
	 * @param element
	 */
	public static void urlFormat(StringBuffer url, String element) {
		url.append("?" + element);
	}

	/**
	 * 12.13dzq 增加请求返回String 取值方法
	 * 
	 * @param result
	 *            http返回数据
	 * @param key
	 *            取数据的键
	 * @return
	 */
	public static Object numberResultJsonFormat(String result, String key) {
		if (result == null)
			return 0;
		JSONObject js = new JSONObject(result);
		return "null".equals(js.get(key).toString()) ? 0 : js.get(key)
				.toString();
	}
	
	/**
	 * bean转为url链接字符串
	 * @author chenjian
	 * @time 2016年12月27日下午8:24:46
	 */
	@SuppressWarnings("unchecked")
	public static String beanConvertUr(StringBuffer url,Object object){
		Gson gson = new Gson();
		String toJson = gson.toJson(object);
		Map<String,String> map= gson.fromJson(toJson, Map.class);
		if(map!=null){
			Iterator<String> mapFor =map.keySet().iterator();
			int num=1;
			while(mapFor.hasNext()){
				String key=mapFor.next();
				Object mapCont=map.get(key)==null?"":map.get(key);
				if(num==1){
					url.append("?"+key+"="+mapCont.toString());
				}else{
					url.append("&"+key+"="+mapCont.toString());
				}
				num++;
			}
		}
		return url.toString();
	}
}
